<?php

class FolderPeer extends BaseFolderNestedSetPeer
{
	public static function recount($folder)
	{
		if($folder)
		{
		
		$c =new Criteria();
		$c->add(Mat2FolderPeer::FOLDER_ID,$folder->getId());
		$c->addJoin(Mat2FolderPeer::MATERIAL_ID,MaterialPeer::ID);
		$c->addAnd(MaterialPeer::PUB,true);
		$c->addAnd(MaterialPeer::TYPE_ID,1);
		$cnt = Mat2FolderPeer::doCount($c);
		$folder->setCount($cnt);
		$folder->save();
		}
	}
	
	
	public static function getFolderIcon($folder,$status)
	{
			
			if($folder->getPhotoPath()!='')
			{
				
			 return image_tag('/uploads/folder/_thumbnail/'.$folder->getPhotoPath());	
			 
			 // return '';
			}
			else 
			{
				if($status=='close')
				{
					//return image_tag('/images/icons/folder_close.gif');
					return '';
				}
				elseif($status=='open')
				{
					//return image_tag('/images/icons/folder_open.gif');
					return '';
				}
				else 
				{
					//return image_tag('/images/icons/folder_empty.gif');
					 return '';
				}
		     
			}
	}
	
	public static function setTags($folder_id,$mat_id)
	{
		
	   	$c = new Criteria();
	   	$c->add(Mat2tagPeer::MAT_ID,$mat_id);
	   	$tags = Mat2tagPeer::doSelect($c);
	   	foreach($tags as $t)
	   	{
	   		$c = new Criteria();
	   		$c->add(Folder2tagPeer::TAG_ID,$t->getTagId());
	   		$c->addAnd(Folder2tagPeer::MAT_ID,$mat_id);
	   		$c->addAnd(Folder2tagPeer::FOLDER_ID,$folder_id);
	   		$cnt = Folder2tagPeer::doCount($c);
	   		if($cnt==0)
	   		{
	   			$f2t = new Folder2tag();
	   			$f2t->setTagId($t->getTagId());
	   			$f2t->setFolderId($folder_id);
	   			$f2t->setMatId($mat_id);
	   			$f2t->save();
	   		}
	   		
	   	}
	   	
	}
	
	/*
	 * Получаем массив с количеством предприятий
	 */
	
	public static function getArrCount($scope)
	{
		$arr = array();
		$c = new Criteria();
		$c->add(FolderPeer::SCOPE,$scope);
		$c->add(FolderPeer::TREE_PARENT,5438);
		$fl = FolderPeer::doSelect($c);
		foreach($fl as $f)
		{
			$c = new Criteria();
			$c->add(MaterialPeer::TYPE_ID,1);
			$c->add(MaterialPeer::PUB,true);
			$c->addJoin(Mat2FolderPeer::MATERIAL_ID,MaterialPeer::ID);
			$c->add(Mat2FolderPeer::FOLDER_ID,$f->getId());
			$cnt = Mat2FolderPeer::doCount($c);
			$arr[$f->getId()] = $cnt;
		}
		return $arr;
		
	}
	
	/*
	 * Получаем массив с VIP предприятиями
	 */
	
	public static function getArrVIP($scope)
	{
		$arr = array();
		$c = new Criteria();
		$c->add(FolderPeer::SCOPE,$scope);
		$c->add(FolderPeer::TREE_PARENT,5438);
		$fl = FolderPeer::doSelect($c);
		foreach($fl as $f)
		{
			$c = new Criteria();
			$c->addJoin(Mat2FolderPeer::MATERIAL_ID,MaterialPeer::ID);
			$c->add(Mat2FolderPeer::FOLDER_ID,$f->getId());
			$c->add(MaterialPeer::TYPE_ID,1);
			$c->add(MaterialPeer::PUB,true);
			$c->addDescendingOrderByColumn(MaterialPeer::UPDATED_AT);
			$c->setLimit(5);
			$vip = MaterialPeer::doSelect($c);
			if($vip)
			{
				
				$av = array();
				$i=0;
				foreach($vip as $v)
				{
					$i++;
					$av[$v->getId()] = "<span style='color: #000; font-size:11px'>".$i.'.&nbsp;'.str_replace('"',"&bdquo",$v->getTitle())."</span>&nbsp;<span style='color: #999; font-size:11px'>".format_date($v->getUpdatedAt(),'d').'</span>';
				}
				  $arr[$f->getId()] = $av;
			}
			
		}
		return $arr;
		
	}
	
		
}
